﻿using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Windows.Forms;
using WinFormsApp1.Common;
using WinFormsApp1.Common.Dto;

namespace WinFormsApp1
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
            label1.BackColor = Color.Transparent;
            label2.BackColor = Color.Transparent;
            label3.BackColor = Color.Transparent;
        }
        public class UsersLogina
        {
            /// <summary>
            /// 用户id 
            /// </summary>
            [Key]
            public int UsersId { get; set; }
            /// <summary>
            /// 用户名称
            /// </summary>
            public string UsersName { get; set; }
            /// <summary>
            /// 角色名称
            /// </summary>
            public string RolesName { get; set; }
        }

        private async void button1_Click(object sender, EventArgs e)
        {
            var userName = this.textBox1.Text;
            var userPwd = this.textBox2.Text;
            UserLogin userLogin = new(userName, userPwd);


            var res = await HttpClientHelper.RequestUrl<UserLogin, ApiResult<UsersLogina>>(URLCommon.Url2, "/Week01Get/UsersLosing", HttpClientHelper.MethodType.POST, userLogin);

            if (res != null)
            {
                UsersLogin.UsersId = res.Data.UsersId;
                UsersLogin.UsersName = res.Data.UsersName;
                UsersLogin.RolesName = res.Data.RolesName;
                if (res.Code == 1)
                {
                    MessageBox.Show("登录成功");
                    Main3 main = new Main3();
                    main.Show();
                    // 关闭登录窗体
                    this.Hide(); // 或者 this.Close(); 如果 完全关闭登录窗体
                }
            }
            else
            {
                MessageBox.Show("登录失败，用户名或密码不正确");
                return;
            }



            //string url = @"http://localhost:5106/api/Week01Get/UsersAdd";
            //HttpClient client = new HttpClient();


            //var content = new StringContent(JsonSerializer.Serialize<UserLogin>(userLogin),Encoding.UTF8);
            //content.Headers.Remove("Content-Type");
            //content.Headers.Add("Content-Type", "application/json");

            //string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;


            //if (result!=null)
            //{
            //    MessageBox.Show("登录成功");
            //    Main main = new Main();
            //    main.Show();
            //    this.Hide();
            //}
            //else
            //{
            //    MessageBox.Show("登录失败");
            //}

        }
        /// <summary>
        /// 跳转页面 到 注册
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            UsersAdd usersAdd = new UsersAdd();
            usersAdd.Show();
            this.Hide();
        }

        public record UserLogin(string UsersName, string UsersPwd);
    }
}
